package com.yuanwu.ncs.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
import com.yuanwu.ncs.DataSourceDTO;
import com.yuanwu.ncs.config.MinioConfig;
import com.yuanwu.ncs.jpa.Student;
import com.yuanwu.ncs.jpa.StudentRepository;
import com.yuanwu.ncs.rabbitmq.NotifyMsgSendVO;
import com.yuanwu.ncs.rabbitmq.QueueConfig;
import com.yuanwu.ncs.service.EtcdService;
import com.yuanwu.ncs.util.MinioUtil;
import io.milvus.client.MilvusServiceClient;
import io.milvus.param.collection.CreateDatabaseParam;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.List;
import java.util.Set;

import static com.yuanwu.ncs.rabbitmq.QueueConfig.NOTIFY_MSG_EXCHANGE;

@RestController
@Slf4j
public class HelloController {


    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private DefaultDataSourceCreator dataSourceCreator;
    @Autowired
    private DataSource dataSource;

    @Autowired
    private StudentRepository studentRepository;

    @GetMapping
    public Set<String> now() {
        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
        return ds.getDataSources().keySet();
    }

    @PostMapping("/add")
    public Set<String> add(@Validated @RequestBody DataSourceDTO dto) {
        DataSourceProperty dataSourceProperty = new DataSourceProperty();
        BeanUtils.copyProperties(dto, dataSourceProperty);
        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
        DataSource dataSource = dataSourceCreator.createDataSource(dataSourceProperty);
        ds.addDataSource(dto.getPoolName(), dataSource);
        return ds.getDataSources().keySet();
    }

    @Autowired
    private EtcdService etcdService;
    @Autowired
    private MilvusServiceClient milvusServiceClient;

    @GetMapping("/hello")
    public Object hello() throws IOException {
//        Expression expression = AviatorEvaluator.getInstance().compileScript("C:\\Users\\P1337892\\IdeaProjects\\springboot_demo01\\src\\main\\java\\com\\yuanwu\\ncs\\aviator\\hello\\hello.av");
//        expression.execute();
        List<Student> all = studentRepository.findAll();
        etcdService.put("yuanwu", "12");
        String yuanwu = etcdService.get("yuanwu");
//        CreateDatabaseParam yuanwuParam = CreateDatabaseParam.newBuilder().withDatabaseName("yuanwu")
//                .build();
//        milvusServiceClient.createDatabase(yuanwuParam);
        return all;
    }

    @Autowired
    private MinioUtil minioUtil;
    @Autowired
    private MinioConfig prop;

    @PostMapping("/upload")
    public ResponseEntity<?> upload(@RequestParam("file") MultipartFile file) {
        String objectName = minioUtil.upload(file);
        if (null != objectName) {
            return ResponseEntity.ok(prop.getEndpoint() + "/" + prop.getBucketName() + "/" + objectName);
        }
        return ResponseEntity.badRequest().build();
    }

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Resource
    private Environment environment;

    @PostMapping("/send/msg")
    public Object send(@RequestBody NotifyMsgSendVO notifyMsgSendVO) {
        log.debug("生产消息【{}】", notifyMsgSendVO);
        rabbitTemplate.convertAndSend(NOTIFY_MSG_EXCHANGE + environment.getProperty("server.port"),
                QueueConfig.NOTIFY_ROUTE_KEY, JSONObject.toJSONString(notifyMsgSendVO));
        return "success";
    }

//    @Resource
//    private HbaseService hbaseService;
//
//    @PostMapping("/hbase/create")
//    public Object send() throws IOException {
//        hbaseService.createTable("stu", "info");
//        return "success";
//    }


}
